<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
 * 用户策略模型
 * @author Cavlin
 */
class User_policies_model extends MY_Model {


	function __construct()
	{
		parent::__construct();
	}

	//获取最后插入id
	public function get_insert_id()
	{
		return $this->db->insert_id();
	}

    /**
     * 获取文件/文件夹的策略及授权
     * @param $uid userid
     * @param $ids 一组文件或文件夹数组
     * @param int $ac 取access_denied的信息 0为可访问,1为不可访问,其它则全部
     * @param int $ftype 1文件/2文件夹
     * @param bool $po 是否获取策略
     * @return mixed
     */
    public function get_access_info($uid, $ids, $ac=1, $ftype=1, $po=FALSE) {
        $field = "f.id,f.access_denied";

        if ($ftype == 2) {
            $myTb = self::TB_USER_FOLDERS;
        } else {
            $myTb = self::TB_USER_FILES;
        }

        if ($po) {
            $field .= ",p.`data`";
            $this->db->join(self::TB_USER_POLICIES." p", "f.id = p.fid AND p.ftype = {$ftype}", "LEFT");
        }

        $where['f.uid'] = $uid;
        if (is_array($ids)) {
            if (count($ids) > 1) {
                $this->db->where_in("f.id", $ids);
            } else {
                $where["f.id"] = $ids[0];
                $this->db->limit(1);
            }
        } else {
            $where["f.id"] = $ids;
            $this->db->limit(1);
        }

        if (($ac == 1) || ($ac == 0)) {
            $where["f.access_denied"] = $ac;
        }

        $this->db->select($field);
        $this->db->where($where);
        $query = $this->db->from($myTb." f")->get();

        return $query->result_array();
    }

    /**
     * @param $fid
     * @param $type
     * @param $uid userid
     * @param $field
     * @return null
     */
    public function get_policy($fid, $type, $uid, $field) {
        if (empty($fid) || empty($type) || empty($uid) || empty($field)) return NULL;

        $where = array(
            'fid'    => $fid,
            'ftype' => $type,
            'uid'    => $uid
        );
        return $this->db->select($field)->get_where(self::TB_USER_POLICIES, $where, 1)->row_array();
    }

    /**
     * 添加策略
     * @param $uid int user id
     * @param $data array policy
     * @return null
     */
    public function add_policy($uid, $data) {
        if(empty($uid) || empty($data)) return;

        $data['uid'] = $uid;
        return $this->db->insert(self::TB_USER_POLICIES, $data);
    }

    /**
     * 更新策略信息
     * @param $fid
     * @param $ftype 类型
     * @param array $policy 策略数组
     * @param array $value 字段组成的数组
     * @return null
     */
    public function update_policy($fid, $ftype, $policy=array(), $value=array()) {
        if (empty($fid) || empty($ftype)) return NULL;

        $where = array(
            'ftype' => $ftype,
            'fid' => $fid
        );
        $data["data"] = json_encode($policy);
        if (is_array($value)) {
            foreach ($value as $k => $v) {
                $data[$k] = $v;
            }
        }
        return $this->db->update(self::TB_USER_POLICIES, $data, $where);
    }

    /**
     * 删除策略
     * @param $uid userid
     * @param $type file type
     * @param $fid file id
     * @return null
     */
    public function del_policy($uid, $type, $fid) {
        if (empty($fid) || empty($uid) || empty($type)) return NULL;

        $where = array('uid' => $uid, 'fid' => $fid, 'ftype' => $type);
        return $this->db->delete(self::TB_USER_POLICIES, $where);
    }

    public function del_policy_nts($type,$fid)
    {
        if (empty($fid) || empty($type)) return NULL;
        $where = array('fid' => $fid, 'ftype' => $type);
        return $this->db->delete(self::TB_USER_POLICIES, $where);
    }
}